package com.sa.app.fragments;

import java.util.List;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.sa.app.R;
import com.sa.app.activities.ItemActivity;
import com.sa.app.objects.Item;
import com.sa.app.utilities.CustomizedOverlay;
import com.sa.app.utilities.MapCircleOverlay;
import com.sa.app.utilities.MyApplication;

public class RegionFragment extends Fragment {

	private MapView map = null;
	private MyLocationOverlay me = null;
	private Boolean isFirstTime = true;
	private View view = null;
	private Drawable drawable;
	private GeoPoint center;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (map == null) {
			// La unica manera que se me ha ocurrido es esta, que cada uno se
			// descomente su linea y comente la otra cuando vaya a utilizar algo
			// del mapa
			// Key API ALE
			map = new MapView(getActivity(),
					"0mjABpkE4XqGtcWzFV8pRyFFqph0tSPL2JBGMaA");
			// Key API VACAS
			// map = new
			// MapView(getActivity(),"0cnkc5rmKBCipb70f9XJ14PcdiCTuzp5TCxs-_Q");

		}

		ItemActivity act = ((ItemActivity) getActivity());
		Item item = act.getItemActual();
		List<List<String>> lista = act.getDataBaseManager().selectHistory(
				item.getNfcID());

		// Preparar para que salga la imagen del Item
		List<Overlay> mapOverlays = map.getOverlays();

		try {
			drawable = MyApplication.resizeImage(new BitmapDrawable(
					getActivity().getApplicationContext().getFilesDir() + "/"
							+ item.getNfcID() + ".png"), 150, 150);

		} catch (Exception e) {
			// TODO: handle exception
			drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
		}
		GeoPoint point; // mal new RegionFragment()
		CustomizedOverlay itemizedoverlay = new CustomizedOverlay(drawable,
				inflater.getContext(), getActivity().getApplication(),
				new RegionFragment());
		for (List<String> r : lista) {
			point = new GeoPoint(Integer.valueOf(r.get(0)), Integer.valueOf(r
					.get(1)));
			MapCircleOverlay om = new MapCircleOverlay(point, Integer.valueOf(r
					.get(2)), "");
			List<Overlay> capas = map.getOverlays();
			capas.add(om);
			OverlayItem overlayitem = new OverlayItem(point, "casa",
					"programando...");
			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedoverlay);
		}
		center = new GeoPoint(((int) (((MyApplication) getActivity()
				.getApplication()).getLocation().getLatitude() * 1E6)),
				(int) (((MyApplication) getActivity().getApplication())
						.getLocation().getLongitude() * 1E6));

		map.postInvalidate();

		if (view == null) {
			view = inflater.inflate(R.layout.map_view, container, false);
		}
		if (((ViewGroup) view.getParent()) != null) {
			((ViewGroup) view.getParent()).removeView(view);
		}
		return view;
	}

	// Esto hace que no cuelgue la aplicación al cambiar de activity utilizando
	// la libreria de compatibilidad
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		setUserVisibleHint(true);
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		GeoPoint center = new GeoPoint(((int) (((MyApplication) getActivity()
				.getApplication()).getLocation().getLatitude() * 1E6)),
				(int) (((MyApplication) getActivity().getApplication())
						.getLocation().getLongitude() * 1E6));
		map.getController().setCenter(center);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		map.setClickable(true);

		map.getController().setCenter(center);
		map.getController().setZoom(15);
		map.setBuiltInZoomControls(true);

		me = new MyLocationOverlay(getActivity(), map);
		map.getOverlays().add(me);

		if (isFirstTime) {
			((ViewGroup) getView()).addView(map);
			view = getView();
			isFirstTime = false;
		}
	}

}
